package com.wanchuang.shengfubao.module.policy;

import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.google.protobuf.InvalidProtocolBufferException;
import com.wanchuang.shengfubao.R;
import com.wanchuang.shengfubao.base.BaseActivity;
import com.wanchuang.shengfubao.base.MainApplication;
import com.wanchuang.shengfubao.net.Apis;
import com.wanchuang.shengfubao.net.NetworkManager;
import com.wanchuang.shengfubao.net.WebApis;
import com.wanchuang.shengfubao.net.interfaces.OnRequestCallBack;
import com.wanchuang.shengfubao.proto.Mystock;
import com.wanchuang.shengfubao.web.ui.WebViewActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PolicyTwoActivity extends BaseActivity {

    @BindView(R.id.tv_message)
    TextView tvMessage;
    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    private List<Fragment> fragmentList;
    private PolicyPeopleFragment politwo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_policy);
        ButterKnife.bind(this);
        tvMessage.setMovementMethod(new ScrollingMovementMethod());
        tvTitle.setText("政策修改");
        backIv.setOnClickListener(v -> finish());
        setMessage();
        initView();
        initListener();
    }


    private void initListener() {
        backIv.setOnClickListener(view -> finish());
        tvTitleRight.setOnClickListener(v -> {
            WebViewActivity.startActivity(mContext, WebApis.HandlingRecords);
        });
    }

    protected void initView() {
        String[] titles = {"政策分配", "政策更改", "政策更改审核"};
        fragmentList = new ArrayList<>();
//        fragmentList.add(policone = PolicyPeopleFragment.NewInstance(1));
        fragmentList.add(politwo = PolicyPeopleFragment.NewInstance(2));
//        fragmentList.add(policthree = PolicyChangeFragment.NewInstance(3));
        viewPager.setOffscreenPageLimit(1);
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return titles[position];
            }

        });
        //设置图标
        tabLayout.setupWithViewPager(viewPager);
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (MainApplication.isRef)
        {
            MainApplication.isRef = false;
            politwo.reRoadData();
        }
    }

    public void setMessage() {
        Mystock.title_list.Builder builder =  Mystock.title_list.newBuilder();
        builder.setType("5");
        Mystock.title_list data = builder.build();
        NetworkManager.INSTANCE.post(Apis.title_list, data.toByteArray(),new OnRequestCallBack() {
            @Override
            public void onOk(byte[] bytes) throws InvalidProtocolBufferException {
                Mystock.title_list result = Mystock.title_list.parseFrom(bytes);
                List<Mystock.goods> list = result.getListDesList();

                StringBuilder des = new StringBuilder();
                for (int i = 0; i < list.size() - 1; i++) {
                    des.append(list.get(i).getGoodsSerial()).append("\n");
                }
                des.append(list.get(list.size() - 1).getGoodsSerial());
                tvMessage.setText(des.toString());

            }

            @Override
            public void onError(int code, String errorMessage) {
                showToast(errorMessage);
            }
        });
    }


}
